Telegram Group & Telegram Channel
Note #19 Reduce sturct size (maligned) 📄

Недавно много говорили, о том что одна и та же структура (type struct), с разными типами полей и с разным порядком, может занимать разное кол-во памяти в зависимости от того, как расположить в памяти компьютера с учетом всех отступов. Более детально прочитать можно в вики [1], почему именно так происходит.

Хотелось бы, чтобы компилятор самостоятельно делал такие оптимизации.

Возьмем структура A:
$ cat main.go
// A is my struct
type A struct {
a bool // 1 byte
b float64 // 8 bytes
c int32 // 4 bytes
}


Хорошо что есть отличный линтер[2], который напомнит нам, что можно что-то улучшить:

$ golangci-lint run --enable=maligned main.go
main.go:4:8: struct of size 24 bytes could be of size 16 bytes (maligned)
type A struct {
^


либо

go get github.com/mdempsky/maligned
$ maligned .
/Users/andrii/work/sg/🇺🇦 Go на двоих/com.golang_for_two/main.go:4:8: struct of size 24 could be 16


Links:
- https://en.wikipedia.org/wiki/Data_structure_alignment
- https://github.com/mdempsky/maligned



tg-me.com/golang_for_two/43
Create:
Last Update:

Note #19 Reduce sturct size (maligned) 📄

Недавно много говорили, о том что одна и та же структура (type struct), с разными типами полей и с разным порядком, может занимать разное кол-во памяти в зависимости от того, как расположить в памяти компьютера с учетом всех отступов. Более детально прочитать можно в вики [1], почему именно так происходит.

Хотелось бы, чтобы компилятор самостоятельно делал такие оптимизации.

Возьмем структура A:

$ cat main.go
// A is my struct
type A struct {
a bool // 1 byte
b float64 // 8 bytes
c int32 // 4 bytes
}


Хорошо что есть отличный линтер[2], который напомнит нам, что можно что-то улучшить:

$ golangci-lint run --enable=maligned main.go
main.go:4:8: struct of size 24 bytes could be of size 16 bytes (maligned)
type A struct {
^


либо

go get github.com/mdempsky/maligned
$ maligned .
/Users/andrii/work/sg/🇺🇦 Go на двоих/com.golang_for_two/main.go:4:8: struct of size 24 could be 16


Links:
- https://en.wikipedia.org/wiki/Data_structure_alignment
- https://github.com/mdempsky/maligned

BY 🇺🇦 Go for two :)


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_for_two/43

View MORE
Open in Telegram


🇺🇦 Go на двоих Telegram | DID YOU KNOW?

Date: |

Traders also expressed uncertainty about the situation with China Evergrande, as the indebted property company has not provided clarification about a key interest payment.In economic news, the Commerce Department reported an unexpected increase in U.S. new home sales in August.Crude oil prices climbed Friday and front-month WTI oil futures contracts saw gains for a fifth straight week amid tighter supplies. West Texas Intermediate Crude oil futures for November rose $0.68 or 0.9 percent at 73.98 a barrel. WTI Crude futures gained 2.8 percent for the week.

Can I mute a Telegram group?

In recent times, Telegram has gained a lot of popularity because of the controversy over WhatsApp’s new privacy policy. In January 2021, Telegram was the most downloaded app worldwide and crossed 500 million monthly active users. And with so many active users on the app, people might get messages in bulk from a group or a channel that can be a little irritating. So to get rid of the same, you can mute groups, chats, and channels on Telegram just like WhatsApp. You can mute notifications for one hour, eight hours, or two days, or you can disable notifications forever.

🇺🇦 Go на двоих from sg


Telegram 🇺🇦 Go for two :)
FROM USA